package RandomHelper

import (
	"crypto/rand"
	"errors"
	"math"
	"math/big"
	rand2 "math/rand"
	"time"
)

func RandomAroundFloat64(min, max float64) float64 {
	rand2.Seed(time.Now().UnixNano())
	res := min + rand2.Float64()*(max-min)
	return res
}

/**
自定义随机因子
*/
func RandomAroundFloat64Seed(seed int64, min, max float64) float64 {
	rand2.Seed(seed)
	res := min + rand2.Float64()*(max-min)
	return res
}

func RandomAroundInt64(min, max int64) (int64, error) {
	if min > max {
		return 0, errors.New("the min is greater than max!")
	}
	//rand.Seed(time.Now().UnixNano())
	if min < 0 {
		f64Min := math.Abs(float64(min))
		i64Min := int64(f64Min)
		result, _ := rand.Int(rand.Reader, big.NewInt(max+1+i64Min))

		return result.Int64() - i64Min, nil
	} else {
		result, _ := rand.Int(rand.Reader, big.NewInt(max-min+1))
		return min + result.Int64(), nil
	}
}

func RandomAroundInt(min, max int) (int, error) {
	if min > max {
		return 0, errors.New("the min is greater than max!")
	}
	rand2.Seed(time.Now().UnixNano())
	return rand2.Intn(max-min) + min, nil
}

/**
自定义随机因子
*/
func RandomAroundIntSeed(seed int64, min, max int) (int, error) {
	if min > max {
		return 0, errors.New("the min is greater than max!")
	}
	rand2.Seed(seed)
	return rand2.Intn(max-min) + min, nil
}

func RandomAroundInt32(min, max int32) (int32, error) {
	if min > max {
		return 0, errors.New("the min is greater than max!")
	}
	rand2.Seed(time.Now().UnixNano())
	return rand2.Int31n(max-min) + min, nil
}

/**
自定义随机因子了
*/
func RandomAroundInt32Seed(seed int64, min, max int32) (int32, error) {
	if min > max {
		return 0, errors.New("the min is greater than max!")
	}
	rand2.Seed(seed)
	return rand2.Int31n(max-min) + min, nil
}
